import api from "@/api";
import { defineStore } from "pinia";

export const useBadgeStroe = defineStore("badge", {
  state: () => ({
    badgeInfoList: [] as Array<{
      id: string;
      author: string;
      content: string;
      createDate: string,
      paragraphId: string,
    }>,
    total: 0,  // 总评论数
    commentCounts: {} as Record<string, number>, // 存储每个段落对应的评论数

  }),

  actions: {

    // 获取徽章内部评论信息
    async getBadgeByParagraphId(data: any) {
      await api.paraBadge.getBadgeInfoList(data).then(res => {
        // console.log("获取徽章内部评论信息:", res);
        this.badgeInfoList = res.data as any;
        this.total = this.badgeInfoList.length
        //更新评论计数映射

      }).catch(error => {
        console.error("获取徽章信息失败:", error);
        throw error;
      });
    },

    // //更新评论计数映射
    // updateCommentCounts(){
    //   this.commentCounts = this.badgeInfoList.reduce((acc,comment)=>{
    //     acc[comment.paragraphIdd] = (acc[comment.paragraphId] || 0)+1;
    //     return acc;
    //   }),{} as Record<string,number>;
    // },
    //新增评论
    async addBadge(data: any) {
      await api.paraBadge.addBadge(data).then(res => {
        console.log("新增评论:", res);
      }).catch(error => {
        console.error("新增评论失败:", error);
      });
    }
  }
})
